package com.liao.dao.impl;

import com.liao.bean.User;
import com.liao.dao.UserDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;


public class UserDaoImpl implements UserDao {
    public User selectUserById(Integer id) throws Exception {
        String resource = "SqlMapConfig.xml";
        InputStream resourceAsStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sessionFactory.openSession();
        User user = sqlSession.selectOne("selectUserById", id);
        sqlSession.close();
        return user;
    }

    @Override
    public List<User> selectUserList() throws Exception {
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("SqlMapConfig.xml"));
        SqlSession sqlSession = sessionFactory.openSession();
        List<User> userList = sqlSession.selectList("selectUserList");
        sqlSession.close();
        return userList;
    }

    @Override
    public List<User> selectUserListByName(String username) throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sessionFactory.openSession();
        List<User> selectUserListByName = sqlSession.selectList("selectUserListByName", username);
        sqlSession.close();
        return selectUserListByName;
    }

    @Override
    public List<User> selectUserListByName2(String username) throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sessionFactory.openSession();
        List<User> selectUserListByName = sqlSession.selectList("selectUserListByName2", username);
        sqlSession.close();
        return selectUserListByName;
    }

    @Override
    public List<User> selectUserListByName3(String username) throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sessionFactory.openSession();
        List<User> selectList = sqlSession.selectList("selectUserListByName3", username);
        sqlSession.close();
        return selectList;
    }

    @Override
    public List<User> selectUserListByName4(String username) throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sessionFactory.openSession();
        List<User> selectList = sqlSession.selectList("selectUserListByName4", username);
        sqlSession.close();
        return selectList;
    }

    @Override
    public void addUser(User user) throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sessionFactory.openSession();
        int addUser = sqlSession.insert("addUser", user);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void addUser2(User user) throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sessionFactory.openSession();
        int addUser = sqlSession.insert("addUser2", user);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void deleteUser(Integer id) throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sessionFactory.openSession();
        sqlSession.delete("deleteUser",id);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void updateUser(User user) throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sessionFactory.openSession();
        sqlSession.update("updateUser",user);
        sqlSession.commit();
        sqlSession.close();
    }
}
